package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.ComplaintMessage;
import com.example.buildtable.pojo.Handle;
import com.example.common.util.R;
import com.example.buildtable.pojo.MessageHandle;
import com.example.buildtable.service.MessageHandleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "留言处理")
@RequestMapping(value = "/messageHandle/")
public class MessageHandleController {
    @Autowired
    private MessageHandleService messageHandleService;
    
    @Operation(summary = "查询所有留言处理数据")
    @GetMapping(value = "/getAllMessageHandle")
    public R getAllMessageHandle() {
        LambdaJoinQueryWrapper<MessageHandle> wrapper = new LambdaJoinQueryWrapper<>(MessageHandle.class);
        wrapper.innerJoin(ComplaintMessage.class, ComplaintMessage::getComplaintMessageId, MessageHandle::getMessageId)
                .innerJoin(Handle.class, Handle::getHandleId, MessageHandle::getHandleId);
        List<MessageHandle> messageHandleList = messageHandleService.list(wrapper);
        return R.ok("查询所有留言处理数据成功").data("messageHandleList", messageHandleList);
    }
    
    @Operation(summary = "增加留言处理")
    @PostMapping(value = "/addMessageHandle")
    public R addMessageHandle(MessageHandle messageHandle) {
        boolean flag = messageHandleService.save(messageHandle);
        if (flag) {
            return R.ok("留言处理成功");
        } else {
            return R.ok("留言处理失败");
        }
    }

    @Operation(summary = "删除留言处理")
    @GetMapping(value = "/deleteMessageHandle/{messageHandleId}")
    public R deleteMessageHandle(@PathVariable("messageHandleId") Integer messageHandleId) {
        boolean flag = messageHandleService.removeById(messageHandleId);
        if (flag) {
            return R.ok("删除留言处理成功");
        } else {
            return R.ok("删除留言处理失败");
        }
    }

    @Operation(summary = "按照留言处理查看")
    @GetMapping(value = "/getMessageHandleById/{messageHandleId}")
    public R getMessageHandleById(@PathVariable("messageHandleId") Integer messageHandleId) {
        LambdaJoinQueryWrapper<MessageHandle> wrapper = new LambdaJoinQueryWrapper<>(MessageHandle.class);
        wrapper.innerJoin(ComplaintMessage.class, ComplaintMessage::getComplaintMessageId, MessageHandle::getMessageId)
                .innerJoin(Handle.class, Handle::getHandleId, MessageHandle::getHandleId);
        wrapper.eq(MessageHandle::getMessageHandleId, messageHandleId);
        MessageHandle messageHandle = messageHandleService.getOne(wrapper);
        if (messageHandle != null) {
            return R.ok("按照留言处理查看成功").data("messageHandle", messageHandle);
        } else {
            return R.error("按照留言处理查看失败");
        }
    }

    @Operation(summary = "修改留言处理")
    @PostMapping(value = "/updateMessageHandle")
    public R updateMessageHandle(MessageHandle messageHandle) {
        boolean flag = messageHandleService.updateById(messageHandle);
        if (flag) {
            return R.ok("修改留言处理成功");
        } else {
            return R.ok("修改留言处理失败");
        }
    }
}